$(function () {
    var dictionaryItem_datagrid = $("#dictionaryItem_datagrid");
    var dictionary_datagrid = $("#dictionary_datagrid");
    var dicItem_dialog = $("#dicItem_dialog");
    var dicItem_form = $("#dicItem_form");

    dictionaryItem_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'name', title: '字典明细编码', width: 100},
            {field: 'dictionary', title: '字典目录名称', width: 100,formatter:function (value) {
                return value.name;
            }},
            {field: 'intro', title: '字典明细介绍', width: 100}
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#dictionaryItem_btns",
        rownumbers:true

    });

    dicItem_dialog.dialog({
        width: 300,
        height: 300,
        buttons: "#dictionaryItem_save_btns",
        closed: true,
        onClose: function () {
            dicItem_form.form('clear');
        }
    });
    $("a[data-item-cmd]").click(function () {
        var methodName = $(this).data('item-cmd');
        methodObject[methodName]();
    });
    var methodObject = {
        add: function () {
            dicItem_dialog.dialog('setTitle', '新增字典明细');
            var row = dictionary_datagrid.datagrid("getSelected");
            if(row != null){
                $("#dictionaryName-combobox").prop("readOnly",true);
                $("#dictionaryName-combobox").combobox('setValue', row.id);
            dicItem_dialog.dialog('open');
            }else{
                $.messager.alert('温馨提示', "请选中字典目录一条数据", 'warning');
            }
        },

        edit: function () {
            var row = dictionaryItem_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.get('/dictionary/selectDictionaryByName.do',{name:row.dictionaryName}, function (data) {
                var ids = $.map(data,function(p){
                    return p.id;
                });
                $("#dictionaryName-combobox").combobox('setValues', ids);
            })
            $("#dictionaryId").show();
            row['dictionary.id'] = row.dictionary.id;
            dicItem_form.form('load', row );
            dicItem_dialog.dialog('setTitle', '编辑字典明细');
            dicItem_dialog.dialog('open');
        },
        del: function () {
            var row = dictionaryItem_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/dictionaryItem/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info');
                            dictionaryItem_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            dictionaryItem_datagrid.datagrid('reload');
        },

        save: function () {
            dicItem_form.form('submit', {
                url: '/dictionaryItem/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info');
                        methodObject.cancel();
                        dictionaryItem_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            dicItem_dialog.dialog('close');
        }

    }
})


